/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._13;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._13
 * @ClassName: RomanToInt
 * @Author: linyaonan
 * @Date: 2019/10/7 16:24
 */
public class RomanToInt {
    public static int romanToInt(String s) {
        return getThis(s, Roman.values());
    }
    private static int getThis(String s, Roman... roman) {
        int result = 0;
        while(s.length() > 0) {
            for (int i = 0; i < roman.length; i++) {
                if(s.startsWith(roman[i].getMessage())) {
                    result += roman[i].getCode();
                    s = s.substring(roman[i].getMessage().length());
                }
            }
        }
        return result;
    }

    enum Roman {

        I(1, "I"),
        V(5, "V"),
        X(10, "X"),
        L(50, "L"),
        C(100, "C"),
        D(500, "D"),
        M(1000, "M"),
        CM(900, "CM"),
        CD(400, "CD"),
        XC(90, "XC"),
        XL(40, "XL"),
        IX(9, "IX"),
        IV(4, "IV"),
        ;
        private Integer code;

        private String message;

        Roman(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public Integer getCode() {
            return this.code;
        }

        public String getMessage() {
            return this.message;
        }
    }
}
